#include <stdio.h>
#include <stdlib.h>

typedef struct binaryTree
{
    /* data */
    int a;
    struct binaryTree *left;
    struct binaryTree *right;
} Tree;

Tree *buildTree(int i)
{
    Tree *head = (Tree *)malloc(sizeof(Tree));
    if (NULL == head)
        exit(-1);
    head->a = i;
    head->left = NULL;
    head->right = NULL;
    return head;
}

void createTreeNode(Tree *node, int i)
{
    if (i < node->a)
    {
        if (node->left != NULL)
        {
            createTreeNode(node->left, i);
        }
        else
        {
            Tree *new = buildTree(i);
            node->left = new;
        }
    }
    else if(i > node->a)
    {
        if (node->right != NULL)
        {
            createTreeNode(node->right, i);
        }
        else
        {
            Tree *new = buildTree(i);
            node->right = new;
        }
    }else
    {
        exit(-1);
    }
}

void ergodicTree(Tree* head)
{
    if(head == NULL)
    {
        return ;
    }
    ergodicTree(head->left);
    ergodicTree(head->right);
    printf("%d\t",head->a);
}

void freeTree(Tree* head)
{
    if(head == NULL)
    return;
    freeTree(head->left);
    freeTree(head->right);
    free(head);
    head = NULL;
}

int main()
{
    Tree *t = buildTree(10);
    createTreeNode(t,5);
    createTreeNode(t,9);
    createTreeNode(t,4);
    createTreeNode(t,20);
    createTreeNode(t,30);
    createTreeNode(t,15);
    createTreeNode(t,7);
    createTreeNode(t,60);
    ergodicTree(t);
    freeTree(t);
    t = NULL; 
    return 0;
}